package com.joney.website.controller;

import com.alibaba.fastjson.JSON;
import com.joney.common.annotation.Auth;
import com.joney.common.exception.DefaultException;
import com.joney.common.pojo.Result;
import com.joney.common.utils.EncryptUtil;
import com.joney.common.utils.JwtToken;
import com.joney.website.dto.request.ConsumeAdd;
import com.joney.website.dto.request.ConsumeQry;
import com.joney.website.dto.request.Login;
import com.joney.website.dto.request.Register;
import com.joney.website.entity.Consume;
import com.joney.website.service.BizService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
* @author wangxianchen
* @create 2018-01-15
* @desc notebook控制器
*/
@Controller
@RequestMapping("/note")
@Log4j2
public class AppController {

    @Autowired
    private BizService bizService;

    @RequestMapping("/login")
    public String login(ModelMap model) {
        model.put("cryptoKey", EncryptUtil.AES_ENCRYPT_KEY);
        return "login";
    }

    @Auth
    @RequestMapping("/main")
    public String main(ModelMap model) {
        model.put("info",bizService.totalCurMonthCost(JwtToken.getLoginUserId()));
        return "main";
    }

    @Auth
    @RequestMapping("/user_center")
    public String userCenter(ModelMap model) {
       // model.put("info",bizService.totalCurMonthCost(JwtToken.getLoginUserId()));
        return "user_center";
    }
    @RequestMapping("/register")
    public String register(ModelMap model) {
        model.put("cryptoKey", EncryptUtil.AES_ENCRYPT_KEY);
        return "register";
    }

    @PostMapping("/login_do")
    @ResponseBody
    public Result loginDo(@RequestParam String param, HttpServletRequest request, HttpServletResponse response) {
        String json = null;
        try {
            json = EncryptUtil.aesDecrypt(param);
        } catch (Exception e) {
            log.error("登录信息解析异常");
            throw new DefaultException("登录信息解析异常");
        }
        log.info(json);
        Login req = JSON.parseObject(json, Login.class);
        return bizService.login(req);
    }

    @PostMapping("/register_do")
    @ResponseBody
    public Result registerDo(@RequestParam String param, HttpServletRequest request, HttpServletResponse response) {
        String json = null;
        try {
            json = EncryptUtil.aesDecrypt(param);
        } catch (Exception e) {
            log.error("注册信息解析异常");
            throw new DefaultException("注册信息解析异常");
        }
        log.info(json);
        Register req = JSON.parseObject(json, Register.class);
        return bizService.register(req);
    }


    @Auth
    @RequestMapping("/consume_list")
    public String consumeList(ModelMap model,ConsumeQry qry) {
        System.out.println(JSON.toJSONString(qry));
        model.put("qry",qry == null ? new ConsumeQry() : qry);
        qry.setUid(JwtToken.getLoginUserId());
        List<Consume> list = bizService.consumeList(qry);
        model.put("list",list);
        model.put("info",bizService.totalInfo(list));
        return "consume_list";
    }

    @Auth
    @RequestMapping("/consume_edit")
    public String consumeAdd(ModelMap model) {
        return "consume_edit";
    }

    @Auth
    @RequestMapping("/consume_save")
    @ResponseBody
    public Result consumeSave(@RequestBody ConsumeAdd req) {
        req.setUid(JwtToken.getLoginUserId());
        return bizService.consumeSave(req);
    }
}
